// cnpm install mongodb
//
var MongoClient = require('mongodb').MongoClient;

var url = 'mongodb://localhost:27017/hello';

MongoClient.connect(url, {useNewUrlParser:true}, (err,db) => {
	if (err) throw err;
	console.log('database created');

	db.createCollection('person', (err, result) => {
		if (err) throw err;
		console.log('collection created');
		db.close();
	});
});

//
url = 'mongodb://localhost:27017/';
MongoClient.connect(url, {useNewUrlParser:true}, (err,db) => {
        if (err) throw err;
	var dbo = db.db('hello');
	var persons = {
		{id: 1, name:'tom1'},
		{id: 2, name:'tom2'},
		{id: 3, name:'tom3'}
	};

	dbo.collection('person').insertMany(persons, (err, res) => {
		if (err) throw err;
		console.log('insert num =>', res.insertedCount);
		db.close();
	});
});


MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("hello");
    dbo.collection("person"). find({}).toArray(function(err, result) { 
        if (err) throw err;
        console.log(result);
        db.close();
    });
});

MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("hello");
    var whereStr = {"name":'xx'};
    dbo.collection("person").find(whereStr).toArray(function(err, result) {
        if (err) throw err;
        console.log(result);
        db.close();
    });
});


MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("hello");
    var whereStr = {"name":'tom'};  // 查询条件
    var updateStr = {$set: { "age" : 10 }};
    dbo.collection("person").updateOne(whereStr, updateStr, function(err, res) {
        if (err) throw err;
        console.log("update succeed.");
        db.close();
    });
});

MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("hello");
    var whereStr = {"name":'en'};  // 查询条件
    var updateStr = {$set: { "age" : 11 }};
    dbo.collection("person").updateMany(whereStr, updateStr, function(err, res) {
        if (err) throw err;
         console.log(res.result.nModified + " docs updated");
        db.close();
    });
});


MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("hello");
    var whereStr = {"name":'tom'};  // 查询条件
    dbo.collection("person").deleteOne(whereStr, function(err, obj) {
        if (err) throw err;
        console.log("doc deleted");
        db.close();
    });
});


MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("hello");
    var whereStr = { name: "en" };  // 查询条件
    dbo.collection("person").deleteMany(whereStr, function(err, obj) {
        if (err) throw err;
        console.log(obj.result.n + " doc deleted");
        db.close();
    });
});


MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("hello");
    var mysort = { age: 10 };
    dbo.collection("person").find().sort(mysort).toArray(function(err, result) {
        if (err) throw err;
        console.log(result);
        db.close();
    });
});


MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("hello");
    dbo.collection("person").find().limit(2).toArray(function(err, result) {
        if (err) throw err;
        console.log(result);
        db.close();
  });
});



MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("hello");
    dbo.collection("person").find().skip(2).limit(2).toArray(function(err, result) {
        if (err) throw err;
        console.log(result);
        db.close();
  });
});


MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
  if (err) throw err;
  var dbo = db.db("hello");
  dbo.collection('orders').aggregate([
    { $lookup:
       {
         from: 'products',            // 右集合
         localField: 'product_id',    // 左集合 join 字段
         foreignField: '_id',         // 右集合 join 字段
         as: 'orderdetails'           // 新生成字段（类型array）
       }
     }
    ]).toArray(function(err, res) {
    if (err) throw err;
    console.log(JSON.stringify(res));
    db.close();
  });
});


MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("hello");
    // 删除 test 集合
    dbo.collection("test").drop(function(err, delOK) {  // 执行成功 delOK 返回 true，否则返回 false
        if (err) throw err;
        if (delOK) console.log("collection deleted");
        db.close();
    });
});
